package com.example.qxfw.common.util;

import com.dmgis.common.AInf;
import com.dmgis.common.LInf;
import com.dmgis.common.Rct;
import com.dmgis.common.TInf;
import com.dmgis.dtm.Grd;
import com.dmgis.layer.DmPrj;
import com.dmgis.mapdisp.*;
import com.dmgis.mapmsi.Mapmsi;
import com.dmgis.work.AreaWork;
import com.example.qxfw.common.dmgis.constant.ContourMapType;
import com.example.qxfw.common.dmgis.constant.RctType;
import com.example.qxfw.common.dmgis.vo.AInfGradeVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class DmgisUtil {

    @Value("${dmgis.slib}")
    private String slibPath;


    /**
     * @param rct 出图范围
     * @param saveImgPath 图片保存路径
     */
    public  <T> String open(T obj, Rct rct, RctType type, String saveImgPath){
        int iWidth=1000;
        int iHeight=1000;
        if (RctType.CAL.equals(type)){
            double dx = rct.Getxmax() - rct.Getxmin();
            double dy = rct.Getymax() - rct.Getymin();
            double dlt = dx / dy;
            iHeight =  (int)(iWidth/dlt);
        }else if (RctType.FIXED.equals(type)){
            double dx = rct.Getxmax() - rct.Getxmin();
            double dy = rct.Getymax() - rct.Getymin();
            double dlt = 1.0;
            double dtemp;
            if (dx / dy > dlt) {
                dtemp = dx / dlt;
                dy = (dtemp - dy) / 2.0;
                rct.Setymin(rct.Getymin() - dy);
                rct.Setymax(rct.Getymax() + dy);
            } else {
                dtemp = dy * dlt;
                dx = (dtemp - dx) / 2.0;
                rct.Setxmin(rct.Getxmin() - dx);
                rct.Setxmax(rct.Getxmax() + dx);
            }
        }


        SymBolSlib symBolSlib = new SymBolSlib();
        symBolSlib.LoadSubGraph(slibPath+"sub.sub");
        LinStySlib linStySlib = new LinStySlib();
        linStySlib.LoadSubGraph(slibPath+"sty.sty");
        PatternSlib patternSlib = new PatternSlib();
        patternSlib.LoadSubGraph(slibPath+"pat.pat");
        FontSlib fontSlib = new FontSlib();
        fontSlib.LoadFontGraph(slibPath+"font.fnt");


        Mapmsi image = new Mapmsi();
        image.CreateImg(iWidth, iHeight, '\u0018');
        long hdc = image.GetDcHandle();
        MapDisp mapDisp = new MapDisp();
        mapDisp.InitData(symBolSlib, linStySlib, patternSlib, fontSlib);
        mapDisp.InitPartWindow2(hdc, 0, 0, iWidth, iHeight, rct);
        mapDisp.DrawFillRect(16777215L, 16777215L, rct, (short)0);
        if (obj != null) {
            if (obj instanceof AreaWork) {
                AreaWork areaWork=(AreaWork) obj;
                mapDisp.DispAreaWork(areaWork, (short) 0);
            }else if (obj instanceof DmPrj){
                DmPrj dmPrj=(DmPrj) obj;
                if (dmPrj.handle!=0){
                    dmPrj.DrawPrj(mapDisp);
                }
                dmPrj.delete();
            }
        }
        String path=saveImgPath+System.currentTimeMillis()+".jpg";
        image.ReleaseHandle(hdc);
        image.SaveJpeg(path);
        image.delete();


        mapDisp.delete();
        symBolSlib.delete();
        linStySlib.delete();
        patternSlib.delete();
        fontSlib.delete();
        rct.Delete();
        return path;
    }



    /**
     * grid等值图分级
     * @param grd
     * @param list
     */
    public static void classification(Grd grd, List<AInfGradeVo> list){
        AInf ainf = new AInf();
        LInf linf = new LInf();

//        TInf tInf = new TInf();
//        tInf.Setszfont((byte) 12);
//        tInf.Sethei(0.12f);
//        tInf.Setwid(0.06f);
//        tInf.Setcol(0);
//        tInf.Setbackclr(0);

        byte flag = 0;
        linf.Setflag(flag);
        byte lclass = 1;
        linf.Setlclass(lclass);
        short ltp = -1;
        linf.Setltp(ltp);
        linf.Setxscale(2);
        linf.Setyscale(2);
        ainf.SetLinf(linf);

        for (int i=0;i<list.size();i++){
            AInfGradeVo aInfGradeVo = list.get(i);
            long rgb = aInfGradeVo.getCol();
            linf.Setclr(rgb);
            ainf.SetCol(rgb);
            ainf.SetPatClr(rgb);
            ainf.SetPatNo((short) i);
//            tInf.Setbackclr(rgb);
            grd.addcontour1(aInfGradeVo.getContourValue(), ainf);
//            grd.addtextcontour(aInfGradeVo.getContourValue(),tInf);
        }

//        tInf.Delete();
        linf.Delete();
        ainf.Delete();
    }


    /**
     * 十进制TO  RGB并转为10进制
     * @param hex
     * @return
     */
    public static Long hexToRGB(String hex){
        int r = Integer.parseInt(hex.substring(0, 2), 16);
        int g = Integer.parseInt(hex.substring(2, 4), 16);
        int b = Integer.parseInt(hex.substring(4), 16);
        return r+g*256L+b*65536L;
    }
}
